import axios from 'axios';
const baseURL ="/api";
// /api = /wxp = http://ganfutong.jiangxi.gov.cn
let http = axios.create({
  baseURL: baseURL,
  withCredentials: false,
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
  },
  transformRequest: [function (data) {
    let newData = '';
    for (let k in data) {
      if (Object.prototype.hasOwnProperty.call(data,k) === true) {
        newData += encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) + '&';
      }
    }
    return newData;
  }]
});

function apiAxios(method, url, params, response) {
  http({
    method: method,
    url: url,
    data: method === 'POST' || method === 'PUT' || method === 'upload' ? params : null,
    params: method === 'GET' || method === 'DELETE' ? params : null,
  }).then(function (res) {
    response(res);
  }).catch(function (err) {
    response(err);
  })
}

let up = axios.create({
  baseURL: baseURL,
  withCredentials: false,
  method: 'POST',
  headers: {
    'Content-Type': 'multipart/form-data'
  }
});
function upload(url,params,response) {
  up({
    url: url,
    data:params? params : null,
  }).then(function (res) {
    response(res);
  }).catch(function (err) {
    response(err);
  })
}
export default {
  get: function (url, params, response) {
    return apiAxios('GET', url, params, response)
  },
  post: function (url, params, response) {
    return apiAxios('POST', url, params, response)
  },
  put: function (url, params, response) {
    return apiAxios('PUT', url, params, response)
  },
  delete: function (url, params, response) {
    return apiAxios('DELETE', url, params, response)
  },
  upload: function(url,params,response){
    return upload(url,params,response)
  }
}
